package com.hz.blog.controller;

import com.hz.api.model.request.ApiResponse;
import com.hz.blog.model.entity.BlogEntity;
import com.hz.blog.service.BlogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    BlogService blogService;

    @PostMapping("/add-blog")
    public String addBlog(@RequestParam("title") String title, @RequestParam("content") String content,
                          @RequestParam("userId") Integer userId, @RequestParam("anonymous") Integer isAnonymous) {
        int add = blogService.addBlog(title, content, userId, isAnonymous==1);
        return add > 0 ? ApiResponse.ok(add) : ApiResponse.intError();
    }

    @GetMapping("/view-blog")
    public String viewBlog(@RequestParam("id") Integer id) {
        BlogEntity blog = blogService.viewBlog(id);
        return blog != null ? ApiResponse.ok(blog) : ApiResponse.resNotFound();
    }

    @PostMapping("/del-blog")
    public String delBlog(@RequestParam("id") Integer id) {
        int del = blogService.delBolg(id);
        return del > 0 ? ApiResponse.ok(del) : ApiResponse.intError();
    }

    @PostMapping("/edit-blog")
    public String editBlog(@RequestParam("id") Integer id, @RequestParam("title") String title,
                           @RequestParam("content") String content) {
        int edit = blogService.editBlog(id, title, content);
        return edit > 0 ? ApiResponse.ok(edit) : ApiResponse.intError();
    }

    @GetMapping("/all-blogs")
    public String allBlogs() {
        return ApiResponse.ok(blogService.allBlogs());
    }

    @GetMapping("/my-blogs")
    public String myBlogs(@RequestParam("userId") Integer userId) {
        return ApiResponse.ok(blogService.myBlogs(userId));
    }


}
